import {
  classBeh
} from '../behavior.js'

const bgm = wx.getBackgroundAudioManager()

Component({
  /**
   * 组件的属性列表
   */
  behaviors: [classBeh],
  properties: {
    country: String
  },
  /**
   * 组件的初始数据
   */
  data: {
    flag: true,
    playorpause: 'images/start.png',
    animetionstyle: ''
  },


  attached: function() {
    this._recoverStatus()
    this._monSwitch()
  },
  // datached() {

  // },


  methods: {
    onplayorpause() {
      if (this.data.flag) {
        bgm.src = this.properties.country
        this.setData({
          animetionstyle: 'animation: lqf linear 5s infinite',
          playorpause: 'images/stop.png',
          flag: false,
        })


      } else {
        this.setData({
          animetionstyle: '',
          playorpause: 'images/start.png',
          flag: true
        })
        bgm.pause()
      }
    },



    _recoverStatus: function() {
      // if (bgm.paused) {
      //   this.setData({
      //     flag: false
      //   })
      //   return
      // }
      // // 这里应该是ajax传来的地址，因为资源短缺，所以就将就了
      // if (bgm.src == this.properties.country) {
      //   this.setData({
      //     flag: true
      //   })

      // }
    },
    _monSwitch: function() {
      bgm.onPlay(() => {
        this._recoverStatus()
      })
      bgm.onPause(() => {
        this._recoverStatus()
      })

      bgm.onStop(() => {
        this._recoverStatus()
      })

      bgm.onEnded(() => {
        this._recoverStatus()
      })




    }


  }


})